package com.example.vendingmachine.controller;

import com.example.vendingmachine.dto.PointDTO;
import com.example.vendingmachine.dto.PointQuery;
import com.example.vendingmachine.entity.Point;
import com.example.vendingmachine.service.PointService;
import com.example.vendingmachine.common.Result;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 点位管理控制器
 */
@RestController
@RequestMapping("/api/points")
@RequiredArgsConstructor
public class PointController {

    private final PointService pointService;

    /**
     * 分页查询点位列表
     */
    @GetMapping
    @PreAuthorize("hasAuthority('point:list')")
    public Result<PageInfo<Point>> list(PointQuery query) {
        return Result.success(pointService.listPoints(query));
    }

    /**
     * 获取点位详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('point:list')")
    public Result<Point> getInfo(@PathVariable Long id) {
        return Result.success(pointService.getPointById(id));
    }

    /**
     * 新增点位
     */
    @PostMapping
    @PreAuthorize("hasAuthority('point:add')")
    public Result<Long> add(@Validated @RequestBody PointDTO pointDTO) {
        return Result.success(pointService.createPoint(pointDTO));
    }

    /**
     * 更新点位
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('point:edit')")
    public Result<Void> update(@PathVariable Long id, @Validated @RequestBody PointDTO pointDTO) {
        pointDTO.setId(id);
        pointService.updatePoint(pointDTO);
        return Result.success();
    }

    /**
     * 删除点位
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('point:delete')")
    public Result<Void> delete(@PathVariable Long id) {
        pointService.deletePoint(id);
        return Result.success();
    }

    /**
     * 更新点位状态
     */
    @PutMapping("/{id}/status")
    @PreAuthorize("hasAuthority('point:edit')")
    public Result<Void> updateStatus(@PathVariable Long id, @RequestParam String status) {
        pointService.updatePointStatus(id, status);
        return Result.success();
    }

    /**
     * 检查区域下是否有点位
     */
    @GetMapping("/check-area")
    @PreAuthorize("hasAuthority('point:list')")
    public Result<Boolean> hasPointsByAreaId(@RequestParam Long areaId) {
        return Result.success(pointService.hasPointsByAreaId(areaId));
    }
} 